package org.itboys.mobile.controller.weixin;

import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.wallet.SundayWalletDto;
import org.itboys.mobile.service.SundayIndexService;
import org.itboys.mobile.service.mysql.front.SundayFrontWalletService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangyang on 2018/1/5.
 * 圣代_首页_controller_weixin
 */
@Controller
@RequestMapping("/sunday/weixin/index")
public class SundayWeiXinIndexController extends BaseController{

    @Autowired
    private SundayIndexService indexService;
    @Autowired
    private SundayFrontWalletService walletService;

    /**
     * 首页
     * @param request
     * @param
     */
    @RequestMapping(value="", method= {RequestMethod.GET,RequestMethod.POST})
    public String index(
            HttpServletRequest request,Model model,
            @RequestParam(value = "type", required = false)Integer type){
        try{
            Long memberId = AccountIdHoldUtils.getSessionMember(request).getId();
            Map<String,Object> result = indexService.getIndexData(request,memberId,type);
            model.addAttribute("result",result);

            if(type==null||type==1){//首页
                //取出所有的image,icon
                return "/aigou/weixin/index/index";
            }else if(type == 4){//全球购/海淘页
                return "/aigou/weixin/index/global";
            }else if(type == 8){//今日疯抢
                return "/aigou/weixin/index/panic";
            }else if(type == 2){//TOP10
                return "/aigou/weixin/index/top10";
            }else if(type == 9){//拼团
                return "/aigou/weixin/index/groups";
            }else if(type ==6){//爱购币
                SundayWalletDto walletDto = walletService.getSingleWalletDto(memberId,false,false,null);
                model.addAttribute("wallet",walletDto);
                return "/aigou/weixin/index/aigou";
            }else if(type == 10){//达人试用
            return "/aigou/weixin/index/shiyong";
             }else if(type == 5){//分销专区
                return "/aigou/weixin/index/fenxiao";
            }
            return "/aigou/weixin/error";
        }catch (Exception e){
            logger.error("/sunday/weixin/index----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }

    /**
     * 底部菜单
     * @param model
     * @param type 1,首页，2分类，3发现.4.购物车，5我的
     * @return
     */
    @RequestMapping(value="/bottom", method= {RequestMethod.GET,RequestMethod.POST})
    public String bottom(Model model,
            @RequestParam(value = "type", required = true)Integer type){

        Map<String,Object> result = indexService.getBottomData();
        model.addAttribute("type",type);
        //单独取出icon
        List<Map<String,Object>> buttons =  (ArrayList<Map<String,Object>>)result.get("buttons");
        for(Map<String,Object> button:buttons){
            model.addAttribute((String)button.get("value"),button);
        }
        return "/aigou/weixin/index/bottom";
    }

}
